var constract = require('../../../utils/constract.js');
var app = getApp();

Page({

  data: {
    num: 0,
    state: 0,
    mode: [
      { visit_type: 0, value: '上门拜访' },
      { visit_type: 1, value: '电话拜访' },
      { visit_type: 2, value: '聊天软件拜访' },
      { visit_type: 3, value: '其他' }
    ],
    visit_type: 0,
    time1: "06:00",
    time2: "06:00",
    isAutoH: true
  },

  onLoad: function (options) {
    var that = this;
    var cardId = options.cardId || '';
    var remarkid = options.remarkid || '';
    var type = options.type || '';
    var state = options.state || '';
    var markType = options.markType || '';
    var name = options.name || '';

    wx.getSystemInfo({
      success: function(res) {
        console.log(res);
        if (res.platform.toUpperCase() == 'ANDROID'){
          that.setData({
            isAutoH: false
          })
        } 
      },
    })

    if(remarkid) {
      that.setData({
        remarkid: remarkid
      })
    }
    this.setData({
      cardId: cardId,
      type: type,
      state: state,
      markType: markType,
      visitors: name
    });
    if(remarkid) {
      app.Verification(function (token) {
        wx.request({
          url: constract.detailRemark,
          header: {token: token},
          data: { id: remarkid },
          success: function (res) {
            if (res.data.data.time && res.data.data.return_visit_time) {
              var date1 = res.data.data.time.split(' ')[0];
              var time1 = res.data.data.time.split(' ')[1];
              var date2 = res.data.data.return_visit_time.split(' ')[0];
              var time2 = res.data.data.return_visit_time.split(' ')[1];
            }
            that.setData({
              content: res.data.data.content,
              visitors: res.data.data.visitors,
              result: res.data.data.result,
              visit_type: res.data.data.visit_type,
              date1: date1,
              time1: time1,
              date2: date2,
              time2: time2,
              num: res.data.data.content.length
            })

          }
        })
      })
    }

    this.getDate();
  },

  radioChange: function (e) {
    var that = this;
    var visit_type = e.detail.value;

    that.setData({
      visit_type: visit_type
    });
  },

  bindinput: function (e) {
    var txt = e.detail.value;
    this.setData({
      num: txt.length
    })
  },

  bindDateChange1: function (e) {
    this.setData({
      date1: e.detail.value
    })
  },

  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
  },

  bindTimeChange1: function(e) {
    this.setData({
      time1: e.detail.value
    })
  },

  bindTimeChange2: function(e) {
    this.setData({
      time2: e.detail.value
    })
  },

  getDate: function () {
    var date = new Date();
    var addMinCount = 5; //当前时间后 5 分钟
    date = date.setMinutes(date.getMinutes() + addMinCount);
    date = new Date(date);

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    hours = hours > 9 ? hours : "0"+hours;
    minutes = minutes > 9 ? minutes : "0"+minutes; 

    var addDayCount = 6; // 7天后,算入小时
    var endDate = date.setDate(date.getDate() + addDayCount);
    endDate = new Date(endDate);
    var endYear = endDate.getFullYear();
    var endMonth = endDate.getMonth() + 1;
    var endDay = endDate.getDate();

    var start = year + '-' + month + '-' + day;
    var end = endYear + '-' + endMonth + '-' + endDay;
    var time = hours+':'+minutes
    
    
    
    this.setData({
      start: start,
      end: end,
      date1: start,
      date2: start,
      time1: time,
      time2: time
    })
  },

  submit: function (e) {
    var that = this;
    var data = e.detail.value;
    var formId = e.detail.formId;
    var len = this.data.num;
    data.formId = formId;

    data.time = data.date1+' '+data.time1;
    data.returnVisitTime = data.date2+' '+data.time2;
    
    var compareTime = data.returnVisitTime.replace(/-/g, '/');
    if (new Date(compareTime) < new Date()) {
      wx.showModal({
        title: '提示',
        content: '回访时间小于当前时间',
        showCancel: false
      });
      return;
    }

    if (this.data.visit_type >= 0) {
      data.visitType = this.data.visit_type;
    }
    if (len < 1 && that.data.markType == 2) {
      wx.showModal({
        title: '提示',
        content: '请输入内容',
        showCancel: false
      });
      return;
    }

    if (wx.showLoading) {
      wx.showLoading({
        title: '保存中',
        mask: true,
      });
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.addRemark,
        data: data,
        header: {token: token},
        success: function (res) {
          if (res.data.code == 12000) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              mask: true,
              duration: 1000
            })
            setTimeout(function () {
              wx.navigateBack();
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: '保存失败',
              showCancel: false
            });
            return;
          }
        },
        fail: function(err){
          wx.showModal({
            title: '提示',
            content: '保存失败',
            showCancel: false
          });
          return;
        },
        complete: function(){
          if (wx.hideLoading) {
            wx.hideLoading();
          }
        }
      })
    })

  }

})